<?php

/**
 * 群组合管理
 */
namespace Api\Controller;
use Api\Controller\HxController;
class AppGroupController extends HxController {
	private $ACTION5 = '5';// 群成员邀请用户加入某群
	private $ACTION6 = '6';// 用户同意加入群
	private $ACTION7 = '7';//  用户拒绝加入群
	private $ACTION8 = '8';//  用户请求加入群
	private $ACTION9 = '9';//  管理员解散了群
	
	private $ACTION10='10';// 群禁言发言
	private $ACTION11='11';// 群解除禁止发言
	
	private $ACTION12='12';// 禁止用户发言
	private $ACTION13='13';// 解除禁止用户发言
	
	protected $model_model;
	protected $user_model;
	protected $user_group;
	public function _initialize() {
		 parent::_initialize();
		 $this->model_model = D("Common/AppGroup");
		 $this->user_model=D("Common/AppUser");
		 $this->user_group=D("Common/UserGroup");
	}
	
	
	/**创建群组*/
	public function create_group(){
	     	$user=$this->_login_varify();
			$group['groupname']=I("groupname",'');
			$group['notice']=I("notice",'');
			$group['descscription']=I("descscription",'');
			$group['is_public']=I('is_public');
			$group['is_allowinvites']=I('is_allowinvites');
			$group['group_uniq_id']=$this->model_model->create_group_uniq_id();
			$group['user_uniq_id']=$user['user_uniq_id'];
			$merbers=I('members',$user['user_uniq_id']);
			if(strstr($merbers, $user['user_uniq_id'])===false){
				$this->ajaxErrorReturn('merbers 参数中 必须包含 创建者的 user_uniq_id');
			}
			$members=array_unique(explode(',',$merbers));// 成员
			
			$data=$this->model_model->create($group);
			if($data){
				$option['groupname']=I("groupname",'');
				$option['desc']=I("descscription");
				$option['public']=I('is_public')==1?true:false;
				$option['approval']=false;// 是否需要审批
				$option['owner']=$user['user_uniq_id'];
				$option['members']=$members;
				$hx_result=$this->createGroups($option);
				
				\Think\Log::write("创建群 create_group--- user_uniq_id=".$user['user_uniq_id'].'groupname='.I("groupname",''));
				if(empty($hx_result['error'])){// 环信接口访问正常
					$data['huanxing_groupid']=$hx_result['data']['groupid'];
					$this->model_model->add($data);
					$this->_add_group_user($members,$group['group_uniq_id'], $user['user_uniq_id']);
				    $this->ajaxSuccessReturn('');
				}else{// 环信接口访问出错
				   \Think\Log::write("创建群 create_group--- user_uniq_id=".$user['user_uniq_id'].'groupname='.I("groupname",''));
				   \Think\Log::write(json_encode($hx_result));
				   $this->ajaxErrorReturn("环信错误代码--error:".$hx_result['error'].'--error_description:'.$hx_result['error_description']);
				}
				
			}else{
				$this->ajaxErrorReturn($this->model_model->getError());
			}
		
	}
	
		/**
		 * 
		 * 群成员邀请用户加入群
		 *  
		 * */
		 function add_group_user_request(){
		 		$user=$this->_login_varify();
				$keywords=I("keywords",'');
				$merbers=explode(',', I("merbers",''));
				$group_uniq_id=I('group_uniq_id','');
				if(empty(I("merbers",''))||empty($merbers)){
					$this->ajaxErrorReturn("merbers 不能为空");
				}
				$group=$this->model_model->where("group_uniq_id='$group_uniq_id'")->find();
				if(empty($group)){
					$this->ajaxErrorReturn("此群不存在");
				}
				$users=M("UserGroup")->where("group_uniq_id='$group_uniq_id'")->getField('user_uniq_id',true);
				if($group['is_allowinvites']==1){//是否允许群成员邀请别人加入此群  1是 2否
					
				}else{
					if(in_array($user['user_uniq_id'], $users)){//属于管理人员
						
					}else{
						$this->ajaxErrorReturn("此群不允许普通成员邀请用户加入");
					}
				}
				
				//发送透传消息
			    $action=$this->getUserName($user).'邀请您加入群'.$group['groupname'];
				$ext['sender']=$user['user_uniq_id'];
				$ext['acceptor']=I("merbers",'');
				$ext['content']=$action;
				$ext['group_uniq_id']=$group['group_uniq_id'];
				$ext['keywords']=$keywords;
				$ext['huanxing_groupid']=$group['huanxing_groupid'];
				$hx_result=$this->yy_hxSendCmd($user['user_uniq_id'], $merbers, $this->ACTION5, $target_type = "users", $ext);
				\Think\Log::write("add_group_user_request 群成员邀请好友加入 群".$group['groupname'].'----'.json_encode($hx_result));
				if(empty($hx_result['error'])){
					$this->ajaxSuccessReturn('',"发送成功");
				}else{
					\Think\Log::write("add_group_user_request 环信错误代码--error:".$hx_result['error'].'--error_description:'.$hx_result['error_description']);
					$this->ajaxErrorReturn("环信错误代码--error:".$hx_result['error'].'--error_description:'.$hx_result['error_description']);
				}
		 }

		/**
		 * 
		 * 用户同意加入群
		 */
		 public function agree_add_group(){
		 		$user=$this->_login_varify();
				$group_uniq_id=I("group_uniq_id","");
				$sender_id=I("sender",'');
				$group=$this->model_model->where("group_uniq_id='$group_uniq_id'")->find();
				if(empty($group)){
					$this->ajaxErrorReturn("群不存在！");
				}
				/*判断是否已经加入该群*/
				$count=M("UserGroup")->where("group_uniq_id='$group_uniq_id'  and  user_uniq_id='$sender_id'")->count(1);
				
				if(empty($count)){//用户主动请求
					$hx_result=$this->addGroupsUser($group['huanxing_groupid'], $sender_id);
					\Think\Log::write('agree_add_group  --'.json_encode($hx_result));
					if(empty($hx_result['error'])){
						
						M("UserGroup")->add(array('user_uniq_id'=>$sender_id,'group_uniq_id'=>$group['group_uniq_id']));
						
				/*查群主和副群主 user_uniq_id*/
				$users=M("UserGroup")->where("group_uniq_id='$group_uniq_id'  and level!=3")->getField("user_uniq_id",true);
		     	//发送透传消息
			    $action=$this->getUserName($user).'同意你入群'.$group['groupname'];
				$ext['sender']=$user['user_uniq_id'];
				$ext['acceptor']=$sender_id;
				$ext['content']=$action;
				$hx_result=$this->yy_hxSendCmd($user['user_uniq_id'], array($sender_id), $this->ACTION6, $target_type = "users", $ext);
			    \Think\Log::write($user['user_uniq_id']."同意你入群 agree_add_group  $group[groupname]--".json_encode($hx_result));
						$this->ajaxSuccessReturn("");
					}else{
						$this->ajaxErrorReturn("环信错误代码--error:".$hx_result['error'].'--error_description:'.$hx_result['error_description']);
					}
				}else{
					//管理员邀请
					$count1=M("UserGroup")->where("group_uniq_id='$group_uniq_id'  and  user_uniq_id='$user[user_uniq_id]'")->count(1);
					if(empty($count1)){
					$hx_result=$this->addGroupsUser($group['huanxing_groupid'], $user['user_uniq_id']);
					\Think\Log::write('agree_add_group  --'.json_encode($hx_result));
					if(empty($hx_result['error'])){
						M("UserGroup")->add(array('user_uniq_id'=>$user['user_uniq_id'],'group_uniq_id'=>$group['group_uniq_id']));
					}else{
						$this->ajaxErrorReturn("环信错误代码--error:".$hx_result['error'].'--error_description:'.$hx_result['error_description']);
					}
				}
				$this->ajaxErrorReturn("您已经加入了该群！");
		  }
		 }
		 /**
		  * 
		  *请求是由 管理人员发起的
		 * 用户拒绝加入群
		 */
		 public function refuse_add_group(){
		 		$user=$this->_login_varify();
				$group_uniq_id=I("group_uniq_id","");
				$sender_id=I("sender",'');
				$group=$this->model_model->where("group_uniq_id='$group_uniq_id'")->find();
				if(empty($group)){
					$this->ajaxErrorReturn("群不存在！");
				}
				/*查群主和副群主 user_uniq_id*/
				$users=M("UserGroup")->where("group_uniq_id='$group_uniq_id'  and level!=3")->getField("user_uniq_id",true);
		     	//发送透传消息
			    $action=$this->getUserName($user).'拒绝加入群'.$group['groupname'];
				$ext['sender']=$user['user_uniq_id'];
				$ext['acceptor']=$sender_id;
				$ext['content']=$action;
				$hx_result=$this->yy_hxSendCmd($user['user_uniq_id'], array($sender_id), $this->ACTION7, $target_type = "users", $ext);
			   \Think\Log::write($user['user_uniq_id']."拒绝加入群 refuse_add_group  $group[groupname]--".json_encode($hx_result));
			   $this->ajaxSuccessReturn('');
		 }
			
			/**用户请求加入群*/
			public function user_request_join(){
				$user=$this->_login_varify();
				$keywords=I("keywords","");
				$group_uniq_id=I("group_uniq_id");
				$group=$this->model_model->where("group_uniq_id='$group_uniq_id'")->find();
				if(empty($group)){
					$this->ajaxErrorReturn("群不存在！");
				}
				if($group['is_public']==1){
				}else{
					$this->ajaxErrorReturn("此群不公开！");
				}
				/*查群主和副群主 user_uniq_id*/
				$users=M("UserGroup")->where("group_uniq_id='$group_uniq_id'  and level!=3")->getField("user_uniq_id",true);
				//发送透传消息
			    $action=$this->getUserName($user).'请求入群'.$group['groupname'];
				$ext['sender']=$user['user_uniq_id'];
				$ext['acceptor']=$users;
				$ext['content']=$action;
				$ext['group_uniq_id']=$group['group_uniq_id'];
				$ext['huanxing_groupid']=$group['huanxing_groupid'];
				$ext['keywords']=$keywords;
				$hx_result=$this->yy_hxSendCmd($user['user_uniq_id'], $users, $this->ACTION8, $target_type = "users", $ext);
				\Think\Log::write($user['user_uniq_id']."用户请求加入群 user_request_join  $group[groupname]--".json_encode($hx_result));
				$this->ajaxSuccessReturn('',"发送成功");
			}
			
			/*
			 * 我的群列表
			 * */
			public function my_group_list(){
				$result=array();
				$user=$this->_login_varify();
				//我的群列表
				$user_groups=$this->user_group->where("user_uniq_id='$user[user_uniq_id]'")->select();
				if(empty($user_groups)==FALSE){// 不为空
					foreach ($user_groups as $key => $value) {
						$in_array[]=$value['group_uniq_id'];						
					}
					// 我加入的群
					$map['group_uniq_id']=array("in",$in_array);
				    $groups=$this->group_k_v($this->model_model->where($map)->select());
					// 单前群的人数统计
					$current_count=$this->group_k_v($this->user_group->field('group_uniq_id,count(1) as current_count')->where($map)->group("group_uniq_id")->select());
					foreach ($user_groups as $key => $value) {
						$group_uniq_id=$value['group_uniq_id'];
						$temp['group_uniq_id']=$group_uniq_id;
						$temp['level']=$value['level'];// 我的等级
						$temp['is_accept_msg']=$value['is_accept_msg'];// 是否接受群消息
						$temp['is_black']=$value['is_black'];
						$temp['huanxing_groupid']=$groups[$group_uniq_id]['huanxing_groupid'];
						$temp['notice']=$groups[$group_uniq_id]['notice'];
						$temp['maxusers']=$groups[$group_uniq_id]['maxusers'];
						$temp['ower_uniq_id']=$groups[$group_uniq_id]['user_uniq_id'];// 群主id
						$temp['groupname']=$groups[$group_uniq_id]['groupname'];// 群名
						$temp['is_public']=$groups[$group_uniq_id]['is_public'];// 是否公开
						$temp['descscription']=$groups[$group_uniq_id]['descscription'];// 简介
						$temp['is_block']=$groups[$group_uniq_id]['is_block'];// 是否禁言 1是 2否
						if($groups[$group_uniq_id]['user_uniq_id']==$user['user_uniq_id']){// 如果是群管理员就不需要禁言
							$temp['is_block']=="1";
						}
						
						$temp['icon']=$groups[$group_uniq_id]['icon'];// 图标
						$temp['current_count']=$current_count[$group_uniq_id]['current_count'];
						$temp['is_join']="1";
						$result[]=$temp;
					}
				}
				$this->ajaxSuccessReturn($result);
			}

			/*
			 * 搜索群列表
			 * */
			public function search_group_list(){
				$result=array();
				$user=$this->_login_varify();
					//我的群列表
				    $user_groups=$this->user_group->where("user_uniq_id='$user[user_uniq_id]'")->select();
					foreach ($user_groups as $key => $value) {
						$in_array[]=$value['group_uniq_id'];						
					}
					$keywords=I("keywords",'');//搜索关键字
				    // 所有群
				    $groups=$this->group_k_v($this->model_model->where("groupname like'%$keywords%' or group_uniq_id='$keywords'")->select());
					if(empty($groups)){
						$this->ajaxSuccessReturn($result);
					}
					// 单前群的人数统计
					$current_count=$this->group_k_v($this->user_group->field('group_uniq_id,count(1) as current_count')->where($map)->group("group_uniq_id")->select());
					foreach ($groups as $key => $value) {
						$group_uniq_id=$key;
						$temp['group_uniq_id']="$group_uniq_id";
						$temp['huanxing_groupid']=$groups[$group_uniq_id]['huanxing_groupid'];
						$temp['maxusers']=$groups[$group_uniq_id]['maxusers'];
						$temp['ower_uniq_id']=$groups[$group_uniq_id]['user_uniq_id'];// 群主id
						$temp['groupname']=$groups[$group_uniq_id]['groupname'];// 群名
						$temp['is_public']=$groups[$group_uniq_id]['is_public'];// 是否公开
						$temp['descscription']=$groups[$group_uniq_id]['descscription'];// 简介
						$temp['is_block']=$groups[$group_uniq_id]['is_block'];// 是否禁言 1是 2否
						if($groups[$group_uniq_id]['user_uniq_id']==$user['user_uniq_id']){// 如果是群管理员就不需要禁言
							$temp['is_block']=="1";
						}
						$temp['icon']=$groups[$group_uniq_id]['icon'];// 图标
						$temp['current_count']=$current_count[$group_uniq_id]['current_count'];
						$temp['notice']=$groups[$group_uniq_id]['notice'];
						$temp['is_join']=in_array($value['group_uniq_id'], $in_array)==false?"2":"1";
						$result[]=$temp;
					}
				$this->ajaxSuccessReturn($result);
			 }
			
			/**
			 * 推荐群列表
			 * */
			 public function recommend_group_list(){
				$result=array();
				$user=$this->_login_varify();
				//我的群列表
				$user_groups=$this->user_group->where("user_uniq_id='$user[user_uniq_id]'")->select();
				if(empty($user_groups)==FALSE){// 不为空
				
					foreach ($user_groups as $key => $value) {
						$in_array[]=$value['group_uniq_id'];						
					}
					// 我没有加入的群
					$map['group_uniq_id']=array("not in",$in_array);
				}
					$map['is_recommend']=array("eq",1);
				    $groups=$this->group_k_v($this->model_model->where($map)->select());
					if(empty($groups)){
						$this->ajaxSuccessReturn($result);
					}
					// 单前群的人数统计
					$current_count=$this->group_k_v($this->user_group->field('group_uniq_id,count(1) as current_count')->where($map)->group("group_uniq_id")->select());
					foreach ($groups as $key => $value) {
						$group_uniq_id=$key;
						$temp['group_uniq_id']=$group_uniq_id;
						$temp['huanxing_groupid']=$groups[$group_uniq_id]['huanxing_groupid'];
						$temp['maxusers']=$groups[$group_uniq_id]['maxusers'];
						$temp['ower_uniq_id']=$groups[$group_uniq_id]['user_uniq_id'];// 群主id
						$temp['groupname']=$groups[$group_uniq_id]['groupname'];// 群名
						$temp['is_public']=$groups[$group_uniq_id]['is_public'];// 是否公开
						$temp['descscription']=$groups[$group_uniq_id]['descscription'];// 简介
						
						$temp['is_block']=$groups[$group_uniq_id]['is_block'];// 是否禁言 1是 2否
						if($groups[$group_uniq_id]['user_uniq_id']==$user['user_uniq_id']){// 如果是群管理员就不需要禁言
							$temp['is_block']=="1";
						}
						
						$temp['icon']=$groups[$group_uniq_id]['icon'];// 图标
						$temp['current_count']=$current_count[$group_uniq_id]['current_count'];
						$temp['notice']=$groups[$group_uniq_id]['notice'];
						$temp['is_join']="2";
						$result[]=$temp;
					}
				
				$this->ajaxSuccessReturn($result);
			
			 }
			
			
			/*

			 * 			 * 群详情
			 * */
			 public function group_detail(){
			 	$user=$this->_login_varify();
				$group_uniq_id=I('group_uniq_id','');
			 	$model=$this->model_model->where("group_uniq_id='$group_uniq_id'")->find();
				unset($model['user_uniq_id']);
				unset($model['id']);
				if(empty($model)){
					$this->ajaxErrorReturn("此群不存在！");
				}else{
					$field="a.user_uniq_id,a.level,a.is_accept_msg,a.is_black,b.icon,b.nick,b.grade,b.picon,a.able_send_msg";
					$list=$this->user_group->alias("a")->field($field)->join(array("left join m_app_user as b on a.user_uniq_id = b.user_uniq_id "))
					->where("a.group_uniq_id=$model[group_uniq_id]")->select();
					$model['user_list']=$list;
				}
				$this->ajaxSuccessReturn($model);
			 }
			 /*修改群信息*/
			 public function group_edit(){
			 	$param_name=I("param_name",'');// 参数名称
				$param_value=I("param_value",'');// 参数值
				$group_uniq_id=I("group_uniq_id",'');
				$user_uniq_id=I('user_uniq_id','');
				$this->_edit_group_proterty($param_name, $param_value, $group_uniq_id,$user_uniq_id);
			 }
			 /*群用户详情*/
			 public function group_user_detail(){
			 	$user=$this->_login_varify();
				$user_uniq_id=I("user_uniq_id",'');
				$group_uniq_id=I("group_uniq_id",'');
				/*查询的对象*/
				$search_user=$this->user_model->where("user_uniq_id='$user_uniq_id'")->find();
				if(empty($search_user)){
					$this->ajaxErrorReturn("此用户不存在！");
				}else{
				     $model['user_uniq_id']=$user_uniq_id;
					 $model['nick']=$search_user["nick"];
					 $model['icon']=$search_user['icon'];
					 $model['grade']=$search_user['grade'];
					 $model['signature']=$search_user['signature'];
					 $model['picon']=$search_user['picon'];				
					 
					 $user_group=$this->user_group->where("user_uniq_id='$user_uniq_id' and group_uniq_id='$group_uniq_id'")->find();
					 $model['level']=$user_group['level'];//群主的等级  1:群主 2副群主 3临时管理员 4普通成员
					 $model['is_black']=$user_group['is_black'];//是否在此群黑名单中    1:在 2不在
					 $model['able_send_msg']=$user_group['able_send_msg'];//是否允许发言  1允许 2 不允许
					 $this->ajaxSuccessReturn($model);
				}
			 }
			 
			 /*群成员列表*/
			 public function group_user_list(){
			 	$result=array();
			 	$user=$this->_login_varify();
				$group_uniq_id=I("group_uniq_id",'');
				$keywords=I('keywords','');
				$fields="a.user_uniq_id,a.level,b.icon,b.nick,b.signature";
				if(empty($keywords)){//不带条件查询
					$result=$this->user_group->alias('a')->field($fields)->join(array("left join  m_app_user as b on a.user_uniq_id=b.user_uniq_id"))->where("a.group_uniq_id='$group_uniq_id'")->order("a.level asc")->select();
				}else{//带条件查询
					$result=$this->user_group->alias('a')->field($fields)->join(array("left join  m_app_user as b on a.user_uniq_id=b.user_uniq_id"))->where("a.group_uniq_id='$group_uniq_id' and (a.user_uniq_id like '%$keywords%' or b.nick like '%$keywords%')")->order("a.level asc")->select();
				}
				$this->ajaxSuccessReturn($result);
			 }
			 
			 /*图片上传*/
			 public function image_upload(){
			 	$this->upload_api();
			 }
			 
			 /**
			  *退出聊天室 
			  */
			 public function exit_group(){
			 	$user=$this->_login_varify();
				$type=I("type",1);//1主动退出  2 管理员踢人
				$user_uniq_id=I("user_uniq_id","");
				$group_uniq_id=I("group_uniq_id","");
				
				$group=$this->model_model->where("group_uniq_id='$group_uniq_id'")->find();
				if($group['user_uniq_id']==$user_uniq_id){
					$this->ajaxErrorReturn("管理员不能进行此操作！");
				}else{
					if($type==1){
						$this->user_group->where("group_uniq_id='$group_uniq_id' and user_uniq_id='$user[user_uniq_id]'")->delete();
						$hx_result=$this->deleteGroupsUser($group['huanxing_groupid'],$user['user_uniq_id']);
						\Think\Log::write("$group_uniq_id 群组减人 $user[user_uniq_id]---环信代码--".json_encode($hx_result));
					}else{
						$admin_user=$this->user_group->where("group_uniq_id='$group_uniq_id' and level<4")->getField("user_uniq_id",true);
						if(in_array($user['user_uniq_id'], $admin_user)){
							$this->user_group->where("group_uniq_id='$group_uniq_id' and user_uniq_id='$user_uniq_id'")->delete();
							$hx_result=$this->deleteGroupsUser($group['huanxing_groupid'],$user_uniq_id);
							\Think\Log::write("$group_uniq_id 群组减人 $user_uniq_id---环信代码--".json_encode($hx_result));	
						}else{
							$this->ajaxErrorReturn("您是普通成员 不能踢成员！");
						}
					}
						$this->ajaxSuccessReturn();
				}
			 }
			 
			 /*解散群*/
			public function dismiss_group(){
			 	$user=$this->_login_varify();
				$group_uniq_id=I("group_uniq_id","");
				$group=$this->model_model->where("group_uniq_id='$group_uniq_id'")->find();
				if($group['user_uniq_id']==$user['user_uniq_id']){
					//发送透传消息
					$acceptor=$this->user_group->where("group_uniq_id='$group_uniq_id'")->getField('user_uniq_id',true);
				    $action=$this->getUserName($user).'解散群'.$group['groupname'];
					$ext['sender']=$user['user_uniq_id'];
					$ext['acceptor']=implode(',',$acceptor);
					$ext['content']=$action;
					$hx_result=$this->yy_hxSendCmd($user['user_uniq_id'], $acceptor, $this->ACTION9, $target_type = "users", $ext);
					\Think\Log::write("解散群传透消息 ".$user['user_uniq_id']."解散群 $group[groupname] 环信代码--".json_encode($hx_result));
					
					$this->user_group->where("group_uniq_id='$group_uniq_id'")->delete();
					$this->model_model->where("group_uniq_id='$group_uniq_id'")->delete();
					$hx_result=$this->deleteGroups($group['huanxing_groupid']);
					\Think\Log::write("$user[user_uniq_id] 解散群 $group_uniq_id  ---环信代码".json_encode($hx_result));
					$this->ajaxSuccessReturn('');
				}else{
					$this->ajaxErrorReturn("您不是管理员 不能进行此操作！");
				}
				
			}

			/*解散我所有创建的群*/
			function  dismiss_all_group(){
				$user=$this->_login_varify();
				$groups=$this->model_model->where("user_uniq_id='$user[user_uniq_id]'")->select();
				foreach($groups as $key=>$value){
					$this->user_group->where("group_uniq_id='$value[group_uniq_id]'")->delete();
					$this->model_model->where("group_uniq_id='$value[group_uniq_id]'")->delete();
					$hx_result=$this->deleteGroups($value['huanxing_groupid']);
				}
				$this->ajaxSuccessReturn('');
			}
			/*添加所有用户到群里*/
			function add_all_user_ingroup(){
				$user=$this->_login_varify();
				$group_uniq_id=I("group_uniq_id",'');
				if(empty($group_uniq_id)){
					$this->ajaxErrorReturn("群group_id不能为空");
					}else{
					$group=$this->model_model->where("group_uniq_id='$group_uniq_id'")->find();
					$group_user=$this->user_group->where("group_uniq_id='$group_uniq_id'")->getField("user_uniq_id",TRUE);
					$all_user=$this->user_model->where("user_uniq_id!='$user[user_uniq_id]'")->getField("user_uniq_id",TRUE);
					$albe_add= array_merge(array_diff($all_user, array_unique($group_user)));
					foreach ($albe_add as $key => $value) {
						$hx_result=$this->addGroupsUser($group['huanxing_groupid'], $value);
						if(empty($hx_result['error'])){
							M("UserGroup")->add(array('user_uniq_id'=>$value,'group_uniq_id'=>$group_uniq_id));	
						}
					}
				}
					$this->ajaxSuccessReturn("","success");
			}
		
	/*获取群背景音乐*/		
	public function video(){
		$date=M("Video")->select();
		$this->ajaxSuccessReturn($date);
	}		
	///////////////////////////////////////////////////////////////////////
	/**
	 * 添加本地群成员
	 * $user_uniq_ids  用户互动号
	 * $group_uniq_id 	群互动号
	 * $ower_uniq_id 群主互动id
	 */
    private function _add_group_user($user_uniq_ids,$group_uniq_id,$ower_uniq_id){
    	foreach ($user_uniq_ids as $key => $value) {
    		if($ower_uniq_id==$value){
    			$datalist[]=array('user_uniq_id'=>$value,'group_uniq_id'=>$group_uniq_id,'level'=>1);	
    		}else{
    			$datalist[]=array('user_uniq_id'=>$value,'group_uniq_id'=>$group_uniq_id,'level'=>4);
    		}
    	}
    	M("UserGroup")->addAll($datalist);
    }
	
	private function _login_varify(){
		$user=sp_get_current_user();
			if($user==false){
				$this->ajaxErrorReturn("请登陆后操作！");
				exit();
			}else{
				return $user;
			}
	}
	
	/*
	 * group array 转 key - value*/
	private function group_k_v($group){
		$result=array();
		foreach($group as $key=>$value){
			$result[$value['group_uniq_id']]=$value;
		}
		return $result;
	}
	
	/**
	 * 修改群属性
	 * @param $param_name 属性名
	 * @param $param_value 属性值
	 * @param $group_uniq_id 群互动号
	 * @param $user_uniq_id  群用户互动号
	 * */
	private function _edit_group_proterty($param_name,$param_value,$group_uniq_id,$user_uniq_id){
		 $user=$this->_login_varify();
		 $able_param_name=array("edit_icon","edit_groupname","edit_acceptmsg","edit_isblock","edit_notice","edit_level","edit_isblack","edit_able_send_msg");
				if(in_array($param_name,$able_param_name)){
					$model=$this->model_model->where("group_uniq_id='$group_uniq_id'")->find();
					$user_group=$this->user_group->where("user_uniq_id='$user_uniq_id' and group_uniq_id='$group_uniq_id'")->find();
					if($param_name=='edit_icon'){// 修改群头像
						$this->model_model->where("group_uniq_id='$group_uniq_id'")->setField('icon',$param_value);
					}elseif($param_name=='edit_groupname'){// 修改群名称
						$this->model_model->where("group_uniq_id='$group_uniq_id'")->setField('groupname',$param_value);
						$hx_result=$this->editGroup($model['huanxing_groupid'],array('groupname'=>$param_value));
						\Think\Log::write("修改$model[huanxing_groupid] 群名称，改为-".$param_value."---环信代码--".json_encode($hx_result));
					}elseif($param_name=='edit_acceptmsg'){//edit_acceptmsg 修改是否接受群消息
						$this->user_group->where("group_uniq_id='$group_uniq_id' and user_uniq_id='$user[user_uniq_id]'")->setField('is_accept_msg',$param_value);
					}elseif($param_name=='edit_isblock'){//修改是否禁言
						$this->_notice_block($param_value,$group_uniq_id);
						$this->model_model->where("group_uniq_id='$group_uniq_id'")->setField('is_block',$param_value);
					}elseif($param_name=='edit_notice'){// 修改群公告
						$this->model_model->where("group_uniq_id='$group_uniq_id'")->setField('notice',$param_value);
					}elseif($param_name=='edit_level'){//以下是管理员设置其他人的转态操作    设置副理员 或临时管理员
						if($user['user_uniq_id']==$user_uniq_id){
							$this->ajaxErrorReturn("不能设置自己的权限");
						}
						//被修改者
						$user_group1=$this->user_group->where("user_uniq_id='$user_uniq_id' and group_uniq_id='$group_uniq_id'")->find();
						// 修改者
						$user_group2=$this->user_group->where("user_uniq_id='$user[user_uniq_id]' and group_uniq_id='$group_uniq_id'")->find();
						if($user_group2['level']<$user_group1["level"]){
							if($user_group2["level"]<I("param_value")){
								$this->user_group->where("group_uniq_id='$group_uniq_id' and user_uniq_id='$user_uniq_id'")->setField('level',$param_value);
								$this->ajaxSuccessReturn("成功");
							}else{
								$this->ajaxErrorReturn("您的的权限不够！");
							}
						}else{
							$this->ajaxErrorReturn("您的的权限不够！");
						}
						
					}elseif($param_name=='edit_isblack'){//添加黑名单
						$this->user_group->where("group_uniq_id='$group_uniq_id' and user_uniq_id='$user_uniq_id'")->setField('is_black',$param_value);
						if($param_value==1){// 添加到黑名单
							$hx_result=$this->setGroupsBlack($model['huanxing_groupid'], $user_uniq_id);
						    \Think\Log::write("$model[huanxing_groupid]添加黑名单 $user_uniq_id"."---环信代码--".json_encode($hx_result));
						}else{//重黑名单移除
							$this->deleteGroupsBlack($model['huanxing_groupid'], $user_uniq_id);
							\Think\Log::write("$model[huanxing_groupid]移除黑名单 $user_uniq_id"."---环信代码--".json_encode($hx_result));
						}
					}elseif($param_name=="edit_able_send_msg"){//修改是否能在群里发言
						$this->_notice_able_send_msg($param_value, $group_uniq_id, $user_uniq_id);
						$this->user_group->where("group_uniq_id='$group_uniq_id' and user_uniq_id='$user_uniq_id'")->setField('able_send_msg',$param_value);
					}
						$this->ajaxSuccessReturn('');
				}else{
					$this->ajaxErrorReturn("参数名称 错误 必须在".implode(',',$able_param_name));
				}
		
	}
	
	/*获取用户名称*/
	function getUserName($user){
		if(empty($user['nick'])){
			return $user['user_uniq_id'];
		}else{
			return $user['nick'];
		}
	}
	
	/*测试session*/
	function test_session(){
		$user=$this->_login_varify();
		var_dump("session:".json_encode($user));
		var_dump("times:".I("time"));
	}
	
	/*禁言 解禁 通知*/
	function _notice_block($value,$group_uniq_id){
				$user=$this->_login_varify();
				//群成员
				$group_user_list=$this->user_group->where("group_uniq_id='$group_uniq_id'")->getField('user_uniq_id',TRUE);
		        //发送透传消息
			   
				$ext['sender']=$user['user_uniq_id'];
				$ext['acceptor']=implode(',', $group_user_list);
				$ext['group_uniq_id']=$group_uniq_id;
				$ext['keywords']='';
				$ext['huanxing_groupid']='';
		
		if($value==1){//禁言操作
		    $ext['content']="此群已经被禁言了";
			$ACTION=$this->ACTION10;
			
		}elseif($value==2){//解禁操作
	     	 $ext['content']="此群可以发言啦";
			$ACTION=$this->ACTION11;
		}
		$hx_result=$this->yy_hxSendCmd($user['user_uniq_id'], $group_user_list, $ACTION, $target_type = "users", $ext);
				\Think\Log::write("禁言 解禁".'----'.json_encode($hx_result));
				if(empty($hx_result['error'])){
					\Think\Log::write("禁言  解禁  成功");
				}else{
					\Think\Log::write("_notice_block 环信错误代码--error:".$hx_result['error'].'--error_description:'.$hx_result['error_description']);
					
		}
	}


	/*禁止用户发言 解除发言*/
	function _notice_able_send_msg($value,$group_uniq_id,$user_uniq_id){
		       $user=$this->_login_varify();
		        //发送透传消息
				$ext['sender']=$user['user_uniq_id'];
				$ext['acceptor']=$user_uniq_id;
				$ext['group_uniq_id']=$group_uniq_id;
				$ext['keywords']='';
				$ext['huanxing_groupid']='';
		
		if($value==1){//禁言操作
		    $ext['content']="你可以发言了";
		    $ACTION=$this->ACTION12;
			
		}elseif($value==2){//可以操作
	     	$ext['content']="你已经被禁言了";
			$ACTION=$this->ACTION13;
		}
		
		$hx_result=$this->yy_hxSendCmd($user['user_uniq_id'], array($user_uniq_id), $ACTION, $target_type = "users", $ext);
				\Think\Log::write("个人禁言发言操作".'----'.json_encode($hx_result));
				if(empty($hx_result['error'])){
					//$this->ajaxSuccessReturn('',"发送成功");
					\Think\Log::write("个人 发言  禁言  成功");
				}else{
					\Think\Log::write("_notice_enable_msg 环信错误代码--error:".$hx_result['error'].'--error_description:'.$hx_result['error_description']);
					//$this->ajaxErrorReturn("环信错误代码--error:".$hx_result['error'].'--error_description:'.$hx_result['error_description']);
		}
	}
	
	public function test(){
		      //发送透传消息
			   
				$ext['sender']='501211249';
				$ext['acceptor']=implode(',', array('501211249','290632470'));
				$ext['group_uniq_id']='757638851';
				$ext['keywords']='';
				$ext['huanxing_groupid']='';
		$hx_result=$this->yy_hxSendCmd('501211249', array('501211249'), $this->ACTION9, $target_type = "users", $ext);
		var_dump($hx_result);
	}
	
	public function test1(){
		//发送透传消息
			    $action='xx邀请您加入群xxx';
				$ext['sender']='290632470';
				$ext['acceptor']='501211249';
				$ext['content']=$action;
				$ext['group_uniq_id']='757638851';
				$ext['keywords']='XXXXX';
				$ext['huanxing_groupid']='11111';
				$hx_result=$this->yy_hxSendCmd('290632470', array('501211249','290632470'), $this->ACTION12, $target_type = "users", $ext);
				var_dump($hx_result);
	}
}

